package cn.b.sky.form.form.builder;

import cn.b.sky.form.model.BusinessField;
import cn.b.sky.form.model.BusinessFieldValue;
import cn.b.sky.form.model.ScreenField;
import cn.b.sky.form.utils.BusinessTypeDefinitionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * <strong>Title : BaseElementBuilder.java<br></strong>
 * <strong>Package : cn.b.sky.form.form.builder<br></strong>
 * <strong>Description : 基本的元素构建器</strong><br> 
 * <strong>Create on : 2016-11-8 下午4:05:44<br></strong>
 * <p>
 * @author lidapeng<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
public abstract class BaseElementBuilder {

  public static final String SHOWITEM = "label";

  public static final String SELECTVALUE = "value";
  
  protected static final String STATICOPTION = "staticoption";
  
  protected static final String DYNAMICOPTION = "dynamicoption";
  
  protected static final int FIELD_DIV_WIDTH = 120;

  protected abstract String buildHtmlElement(ScreenField field);

  public String toHtmlElement(ScreenField field) {
    StringBuilder fieldHtml = new StringBuilder();
    BusinessField bf = field.getBf();
    fieldHtml.append("<dl ");
    if(bf != null){
      String style = BusinessTypeDefinitionUtils.getFieldStyles(bf.getFieldType());
      if(!StringUtils.isBlank(style)){
        fieldHtml.append("class=\"").append(style).append("\"");
      }
    }
    fieldHtml.append(">");
    fieldHtml.append("<dt>");
    if(!StringUtils.isBlank(bf.getFieldLabel())){
      fieldHtml.append(bf.getFieldLabel()).append("：");
    }
    fieldHtml.append("</dt>");
    fieldHtml.append("<dd>");
    fieldHtml.append(buildHtmlElement(field));
    fieldHtml.append("</dd>");
    fieldHtml.append("</dl>");
    return fieldHtml.toString();
  }

  /**
   * 
   * <strong>Title : getClassAttribute<br></strong>
   * <strong>Description : </strong>获取class属性<br> 
   * <strong>Create on : 2016-11-9 下午2:29:04<br></strong>
   * <p>
   * @return
   * String
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  protected String getValidationAttribute(BusinessField field) {
    StringBuilder sb = new StringBuilder(" ");
    if (field.getRequiredFlag() == 1) {
      sb.append("required ");
    }
    String validation = field.getValidationExpression();
    if (!StringUtils.isBlank(validation)) {
      for (String str : validation.split(",")) {
        if (sb.indexOf(str) == -1) {
          sb.append(str).append(" ");
        }
      }
    }
    return sb.toString().trim();
  }
  
  /**
   * 
   * <strong>Title : getKeyValues<br></strong>
   * <strong>Description : </strong>获取数据类型为静态选型和动态选型的选项值<br> 
   * <strong>Create on : 2016-11-11 下午3:50:35<br></strong>
   * <p>
   * @param field
   * @return
   * List<Map<String,Object>>
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  protected List<Map<String, Object>> getKeyValues(BusinessField field) {
    //静态的
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    if (STATICOPTION.equalsIgnoreCase(field.getDataType())) {
      List<BusinessFieldValue> values = field.getBusinessFieldValueList();
      if (values != null && values.size() > 0) {
        for (BusinessFieldValue value : values) {
          Map<String, Object> map = new LinkedHashMap<String, Object>();
          map.put(SHOWITEM, value.getValueText());
          map.put(SELECTVALUE, value.getValue());
          list.add(map);
        }
      }
    }else if(DYNAMICOPTION.equalsIgnoreCase(field.getDataType())) {
      //动态处理方式预留
    }
    return list;
  }
  

}
